package com.xpgk.mvc.codeSet;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.annotation.Service;
import com.xpgk.constant.ConstantInit;
import com.xpgk.constant.ConstantRender;
import com.xpgk.dto.RenderBean;
import com.xpgk.dto.ZtreeNode;
import com.xpgk.mvc.base.BaseService;
import com.xpgk.mvc.dept.Department;
import com.xpgk.mvc.deptStation.DeptStation;
import com.xpgk.mvc.right.Right;
import com.xpgk.mvc.right.RightService;

@Service(name = CodeSetService.serviceName)
public class CodeSetService extends BaseService{
	
	@SuppressWarnings("unused")
	private static final Log log = Log.getLog(RightService.class);

	public static final String serviceName = "codeService";
	
	/**
	 * 获取子节点数据
	 * 
	 * @param cxt
	 * @param parentIds
	 * @return
	 */
	public List<ZtreeNode> childNodeData(String cxt, String ids) {
		List<Code> list = null;
		if (null != ids) {
			String sql = getSqlMy(Code.sqlId_childNode);
			list =  Code.dao.find(sql, ids);

		} else {
			String sql = getSqlMy(Code.sqlId_rootNode);
			list = Code.dao.find(sql);
		}

		List<ZtreeNode> nodeList = new ArrayList<ZtreeNode>();
		ZtreeNode node = null;

		for (Code code : list) {
			node = new ZtreeNode();
			node.setId(code.getStr(Code.column_ids));
			node.setName(code.getStr(Code.column_name));
			node.setIsParent(code.getStr(Code.column_isparent));
			node.setIcon(cxt + ConstantRender.icon_path + code.getStr(Code.column_icon));
			nodeList.add(node);
		}

		return nodeList;
	}
	
	/**
	 * 添加
	 * 
	 * @param pIds
	 * @param names
	 * @param orderIds
	 * @return
	 */
	public RenderBean addCode(Code code) {
		RenderBean result = new RenderBean();
		Code pCode = Code.dao.findById(code.getStr(Code.column_parentids));
		pCode.set(Code.column_isparent, "true").update();

		code.set(Code.column_level, code.getInt(Code.column_level) + 1);
		code.save();
		
		//添加缓存
		Code.cacheAdd(code.getPKValue());

		result.setCode(ConstantRender.render_success_code);
		return result;
	}
	
	/**
	 * 更改
	 * 
	 * @param ids
	 * @param names
	 */
	public RenderBean updateCode(Code code) {
		RenderBean result = new RenderBean();
		code.update();
		//更改缓存
		Code.cacheAdd(code.getPKValue());
		
		result.setCode(ConstantRender.render_success_code);
		return result;
	}
	
	/**
	 * 删除
	 * 
	 * @param ids
	 * @return
	 */
	public String deleteCode(String ids) {
		Code code = Code.dao.findById(ids);

		// 是否存在子节点
		if (code.getStr(Code.column_isparent).equals("true")) {
			return "存在子节点，不能直接删除"; // 存在子节点，不能直接删除
		}

		// 修改上级节点的isparent
		Code pcode = Code.dao.findById(code.getStr(Code.column_parentids));
		String sql = getSqlMy(Code.sqlId_childCount);
		Record record = Db.use(ConstantInit.db_dataSource_main).findFirst(sql, pcode.getPKValue());
		Long counts = record.getNumber("counts").longValue();
		if (counts == 1) {
			pcode.set(Department.column_isparent, "false");
			pcode.update();
		}

		// 删除
		Code.dao.deleteById(ids);
		
		//删除缓存
		Code.cacheRemove(ids,code.getStr(Code.column_parentids));

		return ConstantRender.render_success_code;
	}

}
